package com.zcoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcoj.core.model.PageData;
import com.zcoj.core.model.PageWrap;
import com.zcoj.mapper.UserSysNoticeMapper;
import com.zcoj.pojo.Dto.UserSysNotice;
import com.zcoj.service.UserSysNoticeService;
import com.zcoj.util.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Service实现
 * @author 剩xia的盛夏
 * @date 2023/02/24 16:16
 */
@Service
public class UserSysNoticeServiceImpl implements UserSysNoticeService {

    @Autowired
    private UserSysNoticeMapper userSysNoticeMapper;

    @Override
    public Integer create(UserSysNotice userSysNotice) {
        userSysNoticeMapper.insert(userSysNotice);
        return userSysNotice.getId();
    }

    @Override
    public void deleteById(Integer id) {
        userSysNoticeMapper.deleteById(id);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) return;
        userSysNoticeMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(UserSysNotice userSysNotice) {
        userSysNoticeMapper.updateById(userSysNotice);
    }

    @Override
    public void updateByIdInBatch(List<UserSysNotice> userSysNotices) {
        if (CollectionUtils.isEmpty(userSysNotices)) return;
        for (UserSysNotice userSysNotice: userSysNotices) {
            this.updateById(userSysNotice);
        }
    }

    @Override
    public UserSysNotice findById(Integer id) {
        return userSysNoticeMapper.selectById(id);
    }

    @Override
    public UserSysNotice findOne(UserSysNotice userSysNotice) {
        Wrapper<UserSysNotice> wrapper = new QueryWrapper<>(userSysNotice);
        return userSysNoticeMapper.selectOne(wrapper);
    }

    @Override
    public List<UserSysNotice> findList(UserSysNotice userSysNotice) {
        Wrapper<UserSysNotice> wrapper = new QueryWrapper<>(userSysNotice);
        return userSysNoticeMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<UserSysNotice> findPage(PageWrap<UserSysNotice> pageWrap) {
        IPage<UserSysNotice> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<UserSysNotice> queryWrapper = new QueryWrapper<>(WrapperUtil.blankToNull(pageWrap.getModel()));
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase("DESC")) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(userSysNoticeMapper.selectPage(page, queryWrapper));
    }

    @Override
    public long count(UserSysNotice userSysNotice) {
        Wrapper<UserSysNotice> wrapper = new QueryWrapper<>(userSysNotice);
        return userSysNoticeMapper.selectCount(wrapper);
    }
}
